from datetime import datetime

from django.db import models
from django.contrib.auth import get_user_model

from goods.models import Goods

User = get_user_model()

class ShoppingCars(models.Model):

    user = models.ForeignKey(User, on_delete=models.DO_NOTHING,)
    goods = models.ForeignKey(Goods, on_delete=models.DO_NOTHING,)
    nums = models.IntegerField(default=0)

    add_time = models.DateTimeField(default=datetime.now, verbose_name="添加时间", help_text="添加时间")

    class Meta:
        verbose_name = ("ShoppingCars")
        verbose_name_plural = ("ShoppingCarss")

    def __str__(self):
        return self.name

    def get_absolute_url(self):
        return reverse("ShoppingCars_detail", kwargs={"pk": self.pk})

class OrderInfo(models.Model):

    ORDER_STATUS = (
      ('success', u'成功'),
      ('cancel', u'取消'),
      ('wait', u'待支付'),
    )

    user = models.ForeignKey(User,on_delete=models.DO_NOTHING,)
    order_sn = models.CharField(max_length=30, unique=True)
    trade_no = models.CharField(max_length=100, unique=True)
    pay_status = models.CharField(max_length=100,choices=ORDER_STATUS)
    order_mount = models.IntegerField(default=0)
    pay_time = models.DateTimeField(default=datetime.now, verbose_name="支付时间", help_text="支付时间")

    add_time = models.DateTimeField(default=datetime.now, verbose_name="添加时间", help_text="添加时间")
    
    # 用户信息
    address = models.CharField(max_length=100)
    signer_name = models.CharField(max_length=100)
    singer_mobile = models.CharField(max_length=100)

    class Meta:
        verbose_name = ("OrderInfo")
        verbose_name_plural = ("OrderInfos")

    def __str__(self):
        return self.name

    def get_absolute_url(self):
        return reverse("OrderInfo_detail", kwargs={"pk": self.pk})

class OrderGoods(models.Model):

    order = models.ForeignKey(OrderInfo, on_delete=models.DO_NOTHING,)
    goods = models.ForeignKey(Goods, on_delete=models.DO_NOTHING,)
    goods_num = models.IntegerField(default=0)

    add_time = models.DateTimeField(default=datetime.now, verbose_name="添加时间", help_text="添加时间")

    class Meta:
        verbose_name = ("OrderGoods")
        verbose_name_plural = ("OrderGoodss")

    def __str__(self):
        return self.name

    def get_absolute_url(self):
        return reverse("OrderGoods_detail", kwargs={"pk": self.pk})
